<template>
  <el-container class="bk_full_wapper_pdtb">
    <el-header class="height_auto pdbt">
      <el-row :gutter="6" class="bk_padding_le_rg">
        <el-col :span="12">
          <vxe-input
            v-model="orgschval"
            id="treeSreach"
            placeholder="机构关键字"
            type="search"
            size="mini"
            prefix-icon="fa fa-sitemap"
            @keyup.enter.native="seachTreeQuery"
            class="input_bk"
            @search-click="seachTreeQuery"
          >
          </vxe-input>
        </el-col>
        <el-col :span="12" style="text-align: right;">
          <el-button
            type="text"
            icon="el-icon-refresh"
            size="mini"
            @click="refresh"
          >刷新
          </el-button
          >
        </el-col>
      </el-row>
    </el-header>
    <el-main>

      <template>
        <!--左侧div内容-->
        <vue-ps-tree
          ref="tree"
          id="treeDemo"
          :tree-comp-data="treeComp"
          @onTreeClick="onClick"
          @handleCreated="handleCreated"
          @createRightMenu="createRightMenu"
        />
      </template>

    </el-main>

    <vxe-modal
      v-model="addOrEdit"
      title="增加机构信息"
      min-width="740" width="940px"
      min-height="480"
      size="small" class="defined_height"
    >
      <template v-slot:title>
        <span class="bk_tetile"><i class="el-icon-office-building"></i>{{ addOrEditTitle }}</span>
      </template>
      <el-container>
        <el-main>
          <vxe-form
            :data="addEdit"
            title-align="right"
            title-width="130"
            size="mini"
            prevent-submit
            title-colon
            ref="xForm"
            class="bk_form_box"
            :rules="formRules"
            @submit="save"
          >
            <div class="vxe-row">
              <vxe-form-item title="类型" span="24">
                <vxe-radio-group v-model="addEdit.B0198" @change="getDWBMByType()" size="mini">
                  <vxe-radio-button
                    :disabled="addEdit.B0198==2 && addEdit.addStatus"
                    label="1"
                    content="法人单位"
                  ></vxe-radio-button>
                  <vxe-radio-button
                    label="2"
                    content="内设机构"
                  ></vxe-radio-button>
                  <vxe-radio-button
                    :disabled="addEdit.B0198==2 && addEdit.addStatus"
                    label="3"
                    content="机构分组"
                  ></vxe-radio-button>
                </vxe-radio-group>
              </vxe-form-item>
            </div>
            <div class="vxe-row" v-show="addEdit.B0198 == 1">
              <vxe-form-item title="法人单位编码" field="B0121" span="8">
                <template v-slot="scope">
                  <vxe-input
                    v-model="addEdit.B0121"
                    placeholder="法人单位编码"
                    clearable
                    @input="$refs.xForm.updateStatus(scope)"
                  ></vxe-input>
                </template>
              </vxe-form-item>
              <vxe-form-item title="机构名称" field="NAME" span="8">
                <template v-slot="scope">
                  <vxe-input
                    v-model="addEdit.NAME"
                    placeholder="请输入名机构名称"
                    clearable
                    @input="$refs.xForm.updateStatus(scope)"
                  ></vxe-input>
                </template>
              </vxe-form-item>
              <vxe-form-item title="机构简称" field="B0101B" span="8">
                <template v-slot="scope">
                  <vxe-input
                    v-model="addEdit.B0101B"
                    placeholder="请输入机构简称"
                    clearable
                    @input="$refs.xForm.updateStatus(scope)"
                  ></vxe-input>
                </template>
              </vxe-form-item>
              <vxe-form-item title="所在行政区" field="sex" span="8">
                {{ addEdit.B0117 }}
                <template v-slot>
                  <vxe-input v-model="addEdit.B0117" placeholder="所在行政区" @click="showDIC(0)"></vxe-input>
                </template>
              </vxe-form-item>
              <vxe-form-item title="机构级别" field="sex" span="8">
                {{ addEdit.B0127 }}
                <template v-slot>
                  <vxe-input v-model="addEdit.B0127" placeholder="机构级别" @click="showDIC(1)"></vxe-input>
                </template>
              </vxe-form-item>
              <vxe-form-item title="机构类别" field="sex" span="8">
                {{ addEdit.B0131 }}
                <template v-slot>
                  <!--<vxe-select-->
                  <!--v-model="addEdit.B0131"-->
                  <!--placeholder="请选择"-->
                  <!--clearable-->
                  <!--&gt;-->
                  <!--<vxe-option v-for="item in dDic" :key="item.value" :value="item.key" :label="item.value"></vxe-option>-->
                  <!--</vxe-select>-->
                  <vxe-input v-model="addEdit.B0131" placeholder="机构类别" @click="showDIC(2)"></vxe-input>
                </template>
              </vxe-form-item>
              <vxe-form-item title="隶属关系" field="sex" span="8">
                {{ addEdit.B0124 }}
                <template v-slot>
                  <!--<vxe-select-->
                  <!--v-model="addEdit.B0124"-->
                  <!--placeholder="请选择"-->
                  <!--clearable-->
                  <!--&gt;-->
                  <!--<vxe-option v-for="item in bDic" :key="item.value" :value="item.key" :label="item.value"></vxe-option>-->
                  <!--</vxe-select>-->
                  <vxe-input v-model="addEdit.B0124" placeholder="隶属关系" @click="showDIC(3)"></vxe-input>
                </template>
              </vxe-form-item>
              <vxe-form-item title="机构成立批准日期" field="B0164" span="8">
                <template v-slot="scope">
                  <vxe-input
                    v-model="addEdit.B0164"
                    placeholder="请填写日期"
                    clearable
                    @input="$refs.xForm.updateStatus(scope)"
                  ></vxe-input>
                </template>
              </vxe-form-item>
              <vxe-form-item title="机构成立批准文号" field="name" span="8">
                <template v-slot>
                  <vxe-input
                    v-model="addEdit.B0167"
                    placeholder="请输入文号"
                    clearable
                  ></vxe-input>
                </template>
              </vxe-form-item>
              <el-card shadow="never" class="bk_form" style="margin-top:15px;">
                <h1>参照公务员法管理</h1>
                <vxe-form :data="addEdit" :rules="formRules" ref="subYform">
                  <vxe-form-item
                    title="申请日期"
                    title-width="80"
                    field="B0268"
                    span="6"
                  >
                    <template v-slot="scope">
                      <vxe-input
                        v-model="addEdit.B0268"
                        placeholder="请填写日期"
                        size="mini"
                        @input="$refs.subYform.updateStatus(scope)"
                      ></vxe-input>
                    </template>
                  </vxe-form-item>
                  <vxe-form-item
                    title="申请文号"
                    title-width="80"
                    field="name"
                    span="6"
                  >
                    <vxe-input
                      v-model="addEdit.B0269"
                      placeholder="请输入文号"
                      size="mini"
                      clearable
                    ></vxe-input>
                  </vxe-form-item>
                  <vxe-form-item
                    title="审批日期"
                    title-width="80"
                    field="B0238"
                    span="6"
                  >
                    <template v-slot="scope">
                      <vxe-input
                        v-model="addEdit.B0238"
                        placeholder="请填写日期"
                        size="mini"
                        @input="$refs.subYform.updateStatus(scope)"
                      ></vxe-input>
                    </template>
                  </vxe-form-item>
                  <vxe-form-item
                    title="审批文号"
                    title-width="80"
                    field="name"
                    span="6"
                  >
                    <vxe-input
                      v-model="addEdit.B0239"
                      placeholder="请输入文号"
                      size="mini"
                      clearable
                    ></vxe-input>
                  </vxe-form-item>
                </vxe-form>
              </el-card>
              <el-row>
                <el-col :span="8">
                  <div class="bk_form_wapper">
                    <table
                      width="100%"
                      border="0"
                      cellspacing="0"
                      cellpadding="0"
                      class="table_map"
                    >
                      <tr>
                        <th width="30%" scope="col">职级</th>
                        <th width="35%" scope="col">正职领导</th>
                        <th width="35%" scope="col">副职领导</th>
                      </tr>
                      <tr>
                        <th scope="row">应配职数</th>
                        <td>
                          <el-input-number
                            size="mini"
                            :min="0"
                            v-model="addEdit.B0183"
                          ></el-input-number>
                        </td>
                        <td>
                          <el-input-number
                            size="mini"
                            :min="0"
                            v-model="addEdit.B0185"
                          ></el-input-number>
                        </td>
                      </tr>
                      <tr>
                        <th scope="row">实配职数</th>
                        <td>{{ actual.ZZLD }}</td>
                        <td>{{ actual.FZLD }}</td>
                      </tr>
                    </table>
                  </div>
                </el-col>
                <el-col :span="16">
                  <div class="bk_form_wapper">
                    <table
                      width="100%"
                      border="0"
                      cellspacing="0"
                      cellpadding="0"
                      class="table_map"
                    >
                      <tbody>
                      <tr>
                        <th scope="col">编制类型</th>
                        <th width="17%" scope="col">行政编制</th>
                        <th width="17%" scope="col">
                          参照公务员法管理事业编制
                        </th>
                        <th width="17%" scope="col">其他事业编制</th>
                        <th width="17%" scope="col">工勤编制</th>
                        <th width="17%" scope="col">其他编制</th>
                      </tr>
                      <tr>
                        <th scope="row">编制数</th>
                        <td>
                          <el-input-number
                            size="mini"
                            :min="0"
                            v-model="addEdit.B0227"
                          ></el-input-number>
                        </td>
                        <td>
                          <el-input-number
                            size="mini"
                            :min="0"
                            v-model="addEdit.B0232"
                          ></el-input-number>
                        </td>
                        <td>
                          <el-input-number
                            size="mini"
                            :min="0"
                            v-model="addEdit.B0233"
                          ></el-input-number>
                        </td>
                        <td>
                          <el-input-number
                            size="mini"
                            :min="0"
                            v-model="addEdit.B0236"
                          ></el-input-number>
                        </td>
                        <td>
                          <el-input-number
                            size="mini"
                            :min="0"
                            v-model="addEdit.B0234"
                          ></el-input-number>
                        </td>
                      </tr>
                      <tr>
                        <th scope="row">实有人数</th>
                        <td>{{ actual.XZBZ }}</td>
                        <td>{{ actual.CGSYBZ }}</td>
                        <td>{{ actual.QTBZ }}</td>
                        <td>{{ actual.GQBZ }}</td>
                        <td>{{ actual.QTBZ }}</td>
                      </tr>
                      </tbody>
                    </table>
                  </div>
                </el-col>
              </el-row>
              <el-row>
                <div class="bk_form_wapper">
                  <el-col :span="24">
                    <el-divider content-position="left" class="my_divider"
                    >备注
                    </el-divider
                    >
                    <vxe-textarea
                      v-model="addEdit.B0199"
                      placeholder="请输入备注信息"
                      size="small"
                    ></vxe-textarea>
                  </el-col>
                </div>
              </el-row>
              <div class="bk_form_wapper">
                <el-divider content-position="left" class="my_divider"
                >内设机构配置
                </el-divider
                >
              </div>
              <el-col :span="16">
                <div class="bk_form_wapper">
                  <table
                    width="100%"
                    border="0"
                    cellspacing="0"
                    cellpadding="0"
                    class="table_map"
                  >
                    <tr>
                      <th width="16%" scope="col">职级</th>
                      <th width="28%" scope="col">领导总数</th>
                      <th width="28%" scope="col">正职领导</th>
                      <th width="28%" scope="col">副职领导</th>
                    </tr>
                    <tr>
                      <th scope="row">应配职数</th>
                      <td>
                        <el-input-number :min="0"
                                         size="mini"
                                         v-model="addEdit.B0150"
                        ></el-input-number>
                      </td>
                      <td>
                        <el-input-number :min="0"
                                         size="mini"
                                         v-model="addEdit.B0190"
                        ></el-input-number>
                      </td>
                      <td>
                        <el-input-number :min="0"
                                         size="mini"
                                         v-model="addEdit.B0191A"
                        ></el-input-number>
                      </td>
                    </tr>
                    <tr>
                      <th scope="row">实配职数</th>
                      <td>{{ actual.NSJGLDZZ }}</td>
                      <td>{{ actual.NSJGLDZZ }}</td>
                      <td>{{ actual.NSJGLDFZ }}</td>
                    </tr>
                  </table>
                </div>
              </el-col>
            </div>
            <div class="vxe-row" v-show="addEdit.B0198 == 2">

              <vxe-form-item title="内设机构编码" field="B0121" span="12">
                {{ addEdit.B0121 }}
                <template v-slot="scope">
                  <vxe-input
                    v-model="addEdit.B0121"
                    placeholder="内设机构编码"
                    clearable
                    @input="$refs.xForm.updateStatus(scope)"
                  ></vxe-input>
                </template>
              </vxe-form-item>
              <vxe-form-item title="机构名称" field="NAME" span="12">
                <template v-slot="scope">
                  <vxe-input
                    v-model="addEdit.NAME"
                    placeholder="请输入名机构名称"
                    clearable
                    @input="$refs.xForm.updateStatus(scope)"
                  ></vxe-input>
                </template>
              </vxe-form-item>
              <vxe-form-item title="机构简称" field="B0101B" span="12">
                <template v-slot="scope">
                  <vxe-input
                    v-model="addEdit.B0101B"
                    placeholder="请输入机构简称"
                    clearable
                    @input="$refs.xForm.updateStatus(scope)"
                  ></vxe-input>
                </template>
              </vxe-form-item>

              <vxe-form-item title="机构级别" field="sex" span="12">
                {{ addEdit.B0127 }}
                <template v-slot>
                  <!--<vxe-select-->
                  <!--v-model="addEdit.B0127"-->
                  <!--placeholder="请选择"-->
                  <!--clearable-->
                  <!--&gt;-->
                  <!--<vxe-option v-for="item in cDic" :key="item.value" :value="item.key" :label="item.value"></vxe-option>-->
                  <!--</vxe-select>-->
                  <vxe-input v-model="addEdit.B0127" placeholder="机构级别" @click="showDIC(1)"></vxe-input>
                </template>
              </vxe-form-item>
              <el-row></el-row>
              <el-row>
                <el-col :span="8">
                  <div class="bk_form_wapper">
                    <table
                      width="100%"
                      border="0"
                      cellspacing="0"
                      cellpadding="0"
                      class="table_map"
                    >
                      <tr>
                        <th width="30%" scope="col">职级</th>
                        <th width="35%" scope="col">正职领导</th>
                        <th width="35%" scope="col">副职领导</th>
                      </tr>
                      <tr>
                        <th scope="row">应配职数</th>
                        <td>
                          <el-input-number :min="0"
                                           size="mini"
                                           v-model="addEdit.B0183"
                          ></el-input-number>
                        </td>
                        <td>
                          <el-input-number :min="0"
                                           size="mini"
                                           v-model="addEdit.B0185"
                          ></el-input-number>
                        </td>
                      </tr>
                      <tr>
                        <th scope="row">实配职数</th>
                        <td>{{ actual.ZZLD }}</td>
                        <td>{{ actual.FZLD }}</td>
                      </tr>
                    </table>
                  </div>
                </el-col>
                <el-col :span="16">
                  <div class="bk_form_wapper">
                    <table
                      width="100%"
                      border="0"
                      cellspacing="0"
                      cellpadding="0"
                      class="table_map"
                    >
                      <tbody>
                      <tr>
                        <th scope="col">编制类型</th>
                        <th width="17%" scope="col">行政编制</th>
                        <th width="17%" scope="col">
                          参照公务员法管理事业编制
                        </th>
                        <th width="17%" scope="col">其他事业编制</th>
                        <th width="17%" scope="col">工勤编制</th>
                        <th width="17%" scope="col">其他编制</th>
                      </tr>
                      <tr>
                        <th scope="row">编制数</th>
                        <td>
                          <el-input-number :min="0"
                                           size="mini"
                                           v-model="addEdit.B0227"
                          ></el-input-number>
                        </td>
                        <td>
                          <el-input-number :min="0"
                                           size="mini"
                                           v-model="addEdit.B0232"
                          ></el-input-number>
                        </td>
                        <td>
                          <el-input-number :min="0"
                                           size="mini"
                                           v-model="addEdit.B0233"
                          ></el-input-number>
                        </td>
                        <td>
                          <el-input-number :min="0"
                                           size="mini"
                                           v-model="addEdit.B0236"
                          ></el-input-number>
                        </td>
                        <td>
                          <el-input-number :min="0"
                                           size="mini"
                                           v-model="addEdit.B0234"
                          ></el-input-number>
                        </td>
                      </tr>
                      <tr>
                        <th scope="row">实有人数</th>
                        <td>{{ actual.XZBZ }}</td>
                        <td>{{ actual.CGSYBZ }}</td>
                        <td>{{ actual.QTBZ }}</td>
                        <td>{{ actual.GQBZ }}</td>
                        <td>{{ actual.QTBZ }}</td>
                      </tr>
                      </tbody>
                    </table>
                  </div>
                </el-col>
              </el-row>
              <el-row>
                <div class="bk_form_wapper">
                  <el-col :span="24">
                    <el-divider content-position="left" class="my_divider"
                    >备注
                    </el-divider
                    >
                    <vxe-textarea
                      v-model="addEdit.B0199"
                      placeholder="请输入备注信息"
                      size="small"
                      :autosize="{ minRows: 4 }"
                    ></vxe-textarea>
                  </el-col>
                </div>
              </el-row>
            </div>
            <div class="vxe-row" v-show="addEdit.B0198 == 3">
              <vxe-form-item title="机构分组编码" field="B0121" span="12">
                {{ addEdit.B0121 }}
                <template v-slot="scope">
                  <vxe-input
                    v-model="addEdit.B0121"
                    placeholder="机构分组编码"
                    clearable
                    @input="$refs.xForm.updateStatus(scope)"
                  ></vxe-input>
                </template>
              </vxe-form-item>
              <vxe-form-item title="机构名称" field="NAME" span="12">
                <template v-slot="scope">
                  <vxe-input
                    v-model="addEdit.NAME"
                    placeholder="请输入名机构名称"
                    clearable
                    @input="$refs.xForm.updateStatus(scope)"
                  ></vxe-input>
                </template>
              </vxe-form-item>
              <vxe-form-item style="color: red" title="机构简称" field="B0101B" span="12">
                <template v-slot="scope">
                  <vxe-input
                    v-model="addEdit.B0101B"
                    placeholder="请输入机构简称"
                    clearable
                    @input="$refs.xForm.updateStatus(scope)"
                  ></vxe-input>
                </template>
              </vxe-form-item>
              <vxe-form-item span="12"></vxe-form-item>
              <el-row>
                <div class="bk_form_wapper">
                  <el-col :span="24">
                    <el-divider content-position="left" class="my_divider"
                    >备注
                    </el-divider
                    >
                    <vxe-textarea
                      v-model="addEdit.B0199"
                      placeholder="请输入备注信息"
                      size="small"
                      :autosize="{ minRows: 4 }"
                    ></vxe-textarea>
                  </el-col>
                </div>
              </el-row>
            </div>
            <!--<vxe-button type="submit" status="primary" @click="save">提交</vxe-button>-->
          </vxe-form>
        </el-main>
        <el-footer class="vxe_modal_btn">
          <el-container>
            <el-main>
              <vxe-button status="primary" content="保存" :disabled="saveStatus" size="mini"
                          @click="save(1)"></vxe-button>
              <vxe-button status="primary" content="保存并关闭" size="mini" @click="save(2)"></vxe-button>
            </el-main>
            <el-aside width="60px">
              <vxe-button content="关闭" size="mini" @click="closeDialog"></vxe-button>
            </el-aside>
          </el-container>
        </el-footer>
      </el-container>
    </vxe-modal>

    <dic-panel key=""
               :open.sync="sheetdic.showDicPanel"
               :value="sheetdic.inputvalue"
               :callback="sheetdic.searchValue"
               :params="sheetdic.dicparam"
               :dickey="sheetdic.dickey"/>

  </el-container>
</template>
<script>
import VuePsTree from "@/components/psTree/vuePsTree";
import request from "@/utils/requestutil";
import {getRealUrl} from "@/utils/httpRequet";
import treeExhide from "@/components/ztree/lib/jquery.ztree.exhide.min.js";
import dicPanel from '@/components/utils/dicPanel'

export default {
  components: {
    VuePsTree,
    dicPanel
  },
  data() {
    return {
      formRules: {
        B0164: [
          {
            pattern: /^((^(?:(?:(?:(?=\d{2}(?:(?:[02468][048])|(?:[13579][26])))\d{4}02(?!00)[0-2]\d))|(?:\d{4}(?:(?:(?:(?:0[469])|(?:11))(?!00)(?:(?:[0-2]\d)|(?:30)))|(?:(?:(?:0[13578])|(?:1[02]))(?!00)(?:(?:[0-2]\d)|(?:3[01])))|(?:02(?!(?:00)|(?:29))[0-2]\d))))$)|(\d{4}((0[1-9])|(1[0-2]))))?$/,
          }
        ],
        B0268: [
          {
            pattern: /^((^(?:(?:(?:(?=\d{2}(?:(?:[02468][048])|(?:[13579][26])))\d{4}02(?!00)[0-2]\d))|(?:\d{4}(?:(?:(?:(?:0[469])|(?:11))(?!00)(?:(?:[0-2]\d)|(?:30)))|(?:(?:(?:0[13578])|(?:1[02]))(?!00)(?:(?:[0-2]\d)|(?:3[01])))|(?:02(?!(?:00)|(?:29))[0-2]\d))))$)|(\d{4}((0[1-9])|(1[0-2]))))?$/,
          }
        ],
        B0238: [
          {
            pattern: /^((^(?:(?:(?:(?=\d{2}(?:(?:[02468][048])|(?:[13579][26])))\d{4}02(?!00)[0-2]\d))|(?:\d{4}(?:(?:(?:(?:0[469])|(?:11))(?!00)(?:(?:[0-2]\d)|(?:30)))|(?:(?:(?:0[13578])|(?:1[02]))(?!00)(?:(?:[0-2]\d)|(?:3[01])))|(?:02(?!(?:00)|(?:29))[0-2]\d))))$)|(\d{4}((0[1-9])|(1[0-2]))))?$/,
          }
        ],
        NAME: [{required: true, message: "请输入名机构名称"}],
        B0101B: [{required: true, message: "请输入机构简称"}],
        B0121: [
          {required: true, message: "请输入机构分组编码"},
          {
            pattern: /(^([a-zA-Z0-9]{3}))(([\.]{1}[a-zA-Z0-9]{3})+)*$/,
            message: "机构编码格式错误"
          }
        ],
      },
      activeName: "1",
      orgschval: "",
      addwin: false,
      treeComp: {
        dataUrl: getRealUrl("groupTreeDataUrl", true),
        treeConfigKey: "GROUP_CATEGORY_RS_UNMK",
        async: true,
        level: "SUB",
        treeBuildConfig: {
          isEdit: true,
          isDrag: false,
          editShowType: 2,
        },
        callback: {
          // onClick:this.onNodeClick
        }
      },
      ztreeObj: {},
      treeSreach: "",
      seachObj: {},
      sheetdic: {
        showDicPanel: false,
        inputvalue: "",
        searchValue: () => {
        },
        dicparam: {multiple: false},
        dickey: null
      },
      rylbDic: [],
      addOrEdit: false,
      addOrEditTitle: '新增机构信息',
      addEdit: {
        new_or_edit: "create",
        tabCode: "RS_UNMK",
        ICONCLS: "rs_dw_Cls",
        STATE: "1",
        QYJB: "1",
        TYPE: "UN",
        SID: "",
        B0198: "1",
      },
      actual: {
        ZZFLD: 0,
        GQBZ: 0,
        CGSYBZ: 0,
        NSJGLDZZ: 0,
        FZLD: 0,
        FZFLD: 0,
        ZZLD: 0,
        NSJGZZFLD: 0,
        XZBZ: 0,
        NSJGFZFLD: 0,
        QTBZ: 0,
        NSJGLDFZ: 0,
        QTSYBZ: 0,
      },
      B0198: "",
      saveStatus: false,
      treenode: ""

    };
  },
  mounted() {
    request({
      url: "dicload",
      urlparam: ["DIC_JGXZ"]
    }).then(dt => {
      this.rylbDic = dt.data;
    });
  },
  methods: {
    showOne() {
      let nodes = this.ztreeObj.getNodes();
      if (nodes.length == 0) {
        setTimeout(() => {
          this.showOne();
        }, 40);
      }
      if (nodes.length == 1 && nodes[0].children == undefined) {
        this.ztreeObj.expandNode(nodes[0]);
        this.ztreeObj.selectNode(nodes[0], false)
        // request({
        //   url: getRealUrl("groupTreeDataUrl", true),
        //   data: {
        //     pid: "0",
        //     templet: "4eed407fc3e24d9e9e5356f57306a690",
        //     level: "SUB"
        //   },
        //   method: "post"
        // }).then(res => {
        //   res.data.forEach(r => {
        //     this.ztreeObj.addNodes(nodes[0], r);
        //   });
        //   this.ztreeObj.expandNode(nodes[0], true, false, false);
        //   this.ztreeObj.selectNode(nodes[0],false)
        // })
      }
    },
    createRightMenu(params) {
      params.items = [
        {label: "新建下级机构", onClick: this.addR},
        '-',
        {label: "编辑", onClick: this.editR},
        {label: "删除", onClick: this.deleteR},
        '-',
        {label: "节点刷新", onClick: this.refresh},
        {label: "刷新", onClick: this.refresh},
      ]
      return;
    },
    deleteR(i, n) {
      let that = this;
      let nid = n.id;
      let name = n.name;
      let us = this.$store.state.user.userInfo;
      let deptId = us.deptId;
      if (nid == deptId) {
        return;
      }

      this.$confirm("您确认删除【" + name + "】机构，是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        request({
          url: "judgeSubUnitMore",
          method: "post",
          params: {NID: nid}
        }).then(res => {
          if (!res.hassub) {
            request({
              url: "judgePersonMore",
              method: "post",
              params: {NID: nid}
            }).then(res => {
              if (!res.hassub) {
                request({
                  url: "deleteUnitMore",
                  method: "post",
                  params: {NID: nid}
                }).then(res => {
                  if (res.success) {
                    that.$message.success("删除成功");
                    that.seachTable();
                  } else {
                    that.$message.error("删除失败");
                  }
                  //刷新树
                  this.refresh();
                });
              } else {
                that.$message.error(
                  name + "下级已有人员信息，不能删除！"
                );
              }
            });
          } else {
            that.$message.error(
              name + "下级已有子机构，不能删除！"
            );
          }
        });
      });
    },
    editR(i, n) {
      this.treenode = n.id;
      this.addOrEdit = true;
      this.addOrEditTitle = '编辑机构信息'
      let obj = {ID: n.id, tabCode: "RS_UNMK", new_or_edit: "edit"};
      request({
        url: "group.FormLoadService",
        method: "get",
        params: obj,
      }).then((res) => {
        this.addEdit = res.obj;
        this.B0198 = this.addEdit.B0198
        this.addEdit.B0117 = this.$dicManager.getName("DIC_RS_ZB01", this.addEdit.B0117);
        this.addEdit.B0127 = this.$dicManager.getName("DIC_RS_ZB03", this.addEdit.B0127);
        this.addEdit.B0131 = this.$dicManager.getName("DIC_RS_ZB04", this.addEdit.B0131);
        this.addEdit.B0124 = this.$dicManager.getName("DIC_RS_ZB87", this.addEdit.B0124);
        if (this.B0198 == 2) {
          this.addEdit.addStatus = true
        }
        this.addEdit.new_or_edit = "edit";
        request({
          url: "setSpzs",
          method: "get",
          params: {NID: n.ID, TYPE: res.obj.B0198},
        }).then((res) => {
          this.actual = res.obj;
        });
      });
    },
    addR(i, n) {
      let that = this;
      this.treenode = n.id;
      this.B0198 = n.B0198;
      this.addEdit = {
        new_or_edit: "create",
        tabCode: "RS_UNMK",
        ICONCLS: "rs_dw_Cls",
        STATE: "1",
        QYJB: "1",
        TYPE: "UN",
        SID: "",
        B0198: this.B0198 == "2" ? "2" : "1",
        addStatus: false,
      };
      if (this.B0198 == 2) {
        this.addEdit.addStatus = true
      }
      //如果上级是内设机构，下级只能是内设机构，编辑也只能选择内设机构
      if (this.addEdit.new_or_edit == "edit" && this.B0198 == 2) {
        let obj = {SID: n.id, jgbm: this.B0198};
        request({
          url: "getDWBM",
          method: "get",
          params: obj
        }).then(res => {
          if (res.success) {
            that.$nextTick(() => {
              that.addEdit.B0121 = res.obj;
            })
          } else {
            this.$message.error("获取机构编码错误");
          }
        });
      }
      this.actual = {
        ZZFLD: 0,
        GQBZ: 0,
        CGSYBZ: 0,
        NSJGLDZZ: 0,
        FZLD: 0,
        FZFLD: 0,
        ZZLD: 0,
        NSJGZZFLD: 0,
        XZBZ: 0,
        NSJGFZFLD: 0,
        QTBZ: 0,
        NSJGLDFZ: 0,
        QTSYBZ: 0
      };
      this.addEdit.SID = n.id;
      setTimeout(() => {
        this.addOrEdit = true;
        this.addOrEditTitle = '增加机构信息'
      }, 1000)

    },
    save(i) {
      let flag = false
      let that = this;
      this.$refs.xForm.validate().then(() => {
        // this.$message.success('xForm校验成功')
      }).then(() => {
        return that.$refs.subYform.validate()
      }).then(() => {
        if (this.addEdit.tabCode == null) {
          this.addEdit.tabCode = "RS_UNMK";
        }
        if (this.addEdit.new_or_edit == null) {
          this.addEdit.new_or_edit = "edit";
        }
        this.addEdit.B0117 = this.addEdit.B0117DIC;
        this.addEdit.B0127 = this.addEdit.B0127DIC;
        this.addEdit.B0131 = this.addEdit.B0131DIC;
        this.addEdit.B0124 = this.addEdit.B0124DIC;

        if (this.addEdit.B0198 == 1) {
          this.addEdit.ICONCLS = "rs_dw_Cls";
        }
        if (this.addEdit.B0198 == 2) {
          this.addEdit.ICONCLS = "dash_Cls";
        }
        if (this.addEdit.B0198 == 3) {
          this.addEdit.ICONCLS = "zzxlxwjygl_Cls";
        }
        return request({
          url: "UnitNel",
          method: "post",
          data: this.addEdit
        })
      }).then(res => {
        this.saveStatus = true;
        if (!res.success) {
          this.$message.error("新增错误 ，请重试或联系管理员");
          return;
        }
        if (this.addEdit.new_or_edit == "create") {
          this.$message.success("新增成功");
        } else {
          this.$message.success("修改成功");
        }
        this.addEdit.ID = res.obj.ID;
        this.addEdit.new_or_edit = "edit";
        that.seachTable();
        if (i == 2) {
          this.addOrEdit = false;
        } else {
          if (this.addEdit.B0117) {
            this.addEdit.B0117 = this.$dicManager.getName("DIC_RS_ZB01", this.addEdit.B0117);
          }
          if (this.addEdit.B0127) {
            this.addEdit.B0127 = this.$dicManager.getName("DIC_RS_ZB03", this.addEdit.B0127);
          }
          if (this.addEdit.B0131) {
            this.addEdit.B0131 = this.$dicManager.getName("DIC_RS_ZB04", this.addEdit.B0131);
          }
          if (this.addEdit.B0124) {
            this.addEdit.B0124 = this.$dicManager.getName("DIC_RS_ZB87", this.addEdit.B0124);
          }
        }
        //刷新树
        this.refresh();
        setTimeout(() => {
          this.saveStatus = false;
        }, 2000)
      }).catch(error => {
        this.$message.error('校验输出错误,请检查校验结果是否正确')
      })

      // this.$message.success('成功执行到下方')
      // if (this.addEdit.NAME == null || this.addEdit.NAME == "") {
      //   this.$message.error("机构名称不能为空");
      //   return
      // }
      // if (this.addEdit.B0101B == null || this.addEdit.B0101B == "") {
      //   this.$message.error("机构简称不能为空");
      //   return
      // }
      // var numReg = /(^([a-zA-Z0-9]{3}))(([\.]{1}[a-zA-Z0-9]{3})+)*$/
      // var numRe = new RegExp(numReg)
      // if (!numRe.test(this.addEdit.B0121)) {
      //   this.$message.error("机构编码格式错误")
      //   return
      // }
    },
    closeDialog() {
      this.addOrEdit = false;
    },
    showDIC(i) {
      let that = this;
      if (i == 0) {
        this.sheetdic.dickey = "DIC_RS_ZB01"
        this.sheetdic.inputValue = this.addEdit.B0117;
        this.sheetdic.showDicPanel = true;
        this.sheetdic.searchValue = function (valInfo) {
          that.addEdit.B0117 = valInfo.dispalyVal;
          that.addEdit.B0117DIC = valInfo.value;
        }
      }
      if (i == 1) {
        this.sheetdic.dickey = "DIC_RS_ZB03"
        this.sheetdic.inputValue = this.addEdit.B0127;
        this.sheetdic.showDicPanel = true;
        this.sheetdic.searchValue = function (valInfo) {
          that.addEdit.B0127 = valInfo.dispalyVal;
          that.addEdit.B0127DIC = valInfo.value;
        }
      }
      if (i == 2) {
        this.sheetdic.dickey = "DIC_RS_ZB04"
        this.sheetdic.inputValue = this.addEdit.B0131;
        this.sheetdic.showDicPanel = true;
        this.sheetdic.searchValue = function (valInfo) {
          that.addEdit.B0131 = valInfo.dispalyVal;
          that.addEdit.B0131DIC = valInfo.value;
        }
      }
      if (i == 3) {
        this.sheetdic.dickey = "DIC_RS_ZB87"
        this.sheetdic.inputValue = this.addEdit.B0124;
        this.sheetdic.showDicPanel = true;
        this.sheetdic.searchValue = function (valInfo) {
          that.addEdit.B0124 = valInfo.dispalyVal;
          that.addEdit.B0124DIC = valInfo.value;
        }
      }
    },
    getDWBMByType() {
      if (this.addEdit.new_or_edit != "edit" && (this.addEdit.B0198 == "2" || this.addEdit.B0198 == "3")) {
        this.addOrEdit = true;
        this.addOrEditTitle = '编辑机构信息'
        let obj = {SID: this.treenode, jgbm: this.addEdit.B0198};
        request({
          url: "getDWBM",
          method: "get",
          params: obj
        }).then(res => {
          if (res.success) {
            this.addEdit.B0121 = res.obj;
            this.$forceUpdate()
          } else {
            this.$message.error("获取机构编码错误");
          }
        });
      }
    },
    onClick(e, treeid, node) {
      this.$store.dispatch("business/setValue", {
        root: "Tree",
        key: "treeobj",
        value: node
      });
      this.$emit("treeNodeClick", ...arguments);
    },
    refresh() {
      this.$refs.tree.refresh();
      if (this.orgschval == "") {
        this.showOne();
      } else {
        this.seachTreeQuery();
      }
    },
    onTreeDblClick(event, treeId, treeNode) {
      // console.log(treeNode)
    },
    seachTreeQuery() {
      if (this.orgschval == "") {
        this.refresh();
        return;
      }
      this.treeComp.level = "ALL";

      clearTimeout(this.timer);
      this.timer = setTimeout(() => {
        // this.ztreeObj.expandAll(true);
        let nodes = this.ztreeObj.transformToArray(this.ztreeObj.getNodes());
        if (nodes.length < 2) {
          this.seachTreeQuery();
        } else {
          var expandNodeLevel = this.ztreeObj.getNodes();
          if (expandNodeLevel.length > 0) {
            for (var i = 0; i < nodes.length; i++) {
              // this.ztreeObj.expandNode(expandNodeLevel[i], true, true, false);
            }
          }
          this.ztreeObj.checkAllNodes(true);
          this.fuzzySearch(this.ztreeObj, "#treeSreach", null, true);
        }
      }, 40);
    },
    handleCreated(ztreeObj) {
      if (ztreeObj) {
        this.ztreeObj = ztreeObj;
        this.showOne();
      }
      this.$emit("handleCreated", ztreeObj);
    },
    fuzzySearch: function (zTreeObj, searchField, isHighLight, isExpand) {
      var nameKey = zTreeObj.setting.data.key.name; //get the key of the node name
      isHighLight = isHighLight === false ? false : true; //default true, only use false to disable highlight
      isExpand = isExpand ? true : false; // not to expand in default
      zTreeObj.setting.view.nameIsHTML = isHighLight; //allow use html in node name for highlight use

      var metaChar = "[\\[\\]\\\\\^\\$\\.\\|\\?\\*\\+\\(\\)]"; //js meta characters
      var rexMeta = new RegExp(metaChar, "gi"); //regular expression to match meta characters

      // keywords filter function
      function ztreeFilter(zTreeObj, _keywords, callBackFunc) {
        if (!_keywords) {
          _keywords = ""; //default blank for _keywords
        }

        // function to find the matching node
        function filterFunc(node) {
          if (node && node.oldname && node.oldname.length > 0) {
            node[nameKey] = node.oldname; //recover oldname of the node if exist
          }
          zTreeObj.updateNode(node); //update node to for modifications take effect
          if (_keywords.length == 0) {
            //return true to show all nodes if the keyword is blank
            // debugger;
            zTreeObj.showNode(node);
            zTreeObj.expandNode(node, isExpand);
            return true;
          }
          //transform node name and keywords to lowercase
          if (
            node[nameKey] &&
            node[nameKey].toLowerCase().indexOf(_keywords.toLowerCase()) != -1
          ) {
            if (isHighLight) {
              //highlight process
              //a new variable 'newKeywords' created to store the keywords information
              //keep the parameter '_keywords' as initial and it will be used in next node
              //process the meta characters in _keywords thus the RegExp can be correctly used in str.replace
              var newKeywords = _keywords.replace(rexMeta, function (matchStr) {
                //add escape character before meta characters
                return "\\" + matchStr;
              });
              node.oldname = node[nameKey]; //store the old name
              var rexGlobal = new RegExp(newKeywords, "gi"); //'g' for global,'i' for ignore case
              //use replace(RegExp,replacement) since replace(/substr/g,replacement) cannot be used here
              node[nameKey] = node.oldname.replace(rexGlobal, function (
                originalText
              ) {
                //highlight the matching words in node name
                var highLightText =
                  '<span style="color: whitesmoke;background-color: darkred;">' +
                  originalText +
                  "</span>";
                return highLightText;
              });
              zTreeObj.updateNode(node); //update node for modifications take effect
            }
            // debugger;
            zTreeObj.showNode(node); //show node with matching keywords
            return true; //return true and show this node
          }
          //  debugger;
          zTreeObj.hideNode(node); // hide node that not matched
          return false; //return false for node not matched
        }

        var nodesShow = zTreeObj.getNodesByFilter(filterFunc); //get all nodes that would be shown
        processShowNodes(nodesShow, _keywords); //nodes should be reprocessed to show correctly
      }

      /**
       * reprocess of nodes before showing
       */
      function processShowNodes(nodesShow, _keywords) {
        if (nodesShow && nodesShow.length > 0) {
          //process the ancient nodes if _keywords is not blank
          if (_keywords.length > 0) {
            $.each(nodesShow, function (n, obj) {
              var pathOfOne = obj.getPath(); //get all the ancient nodes including current node
              if (pathOfOne && pathOfOne.length > 0) {
                //i < pathOfOne.length-1 process every node in path except self
                for (var i = 0; i < pathOfOne.length - 1; i++) {
                  zTreeObj.showNode(pathOfOne[i]); //show node
                  zTreeObj.expandNode(pathOfOne[i], true); //expand node
                }
              }
            });
          } else {
            //show all nodes when _keywords is blank and expand the root nodes
            var rootNodes = zTreeObj.getNodesByParam("level", "0"); //get all root nodes
            $.each(rootNodes, function (n, obj) {
              zTreeObj.expandNode(obj, true); //expand all root nodes
            });
          }
        }
      }

      searchNodeLazy(this.orgschval);
      var timeoutId = null;
      var lastKeyword = "";

      // excute lazy load once after input change, the last pending task will be cancled
      function searchNodeLazy(_keywords) {
        if (timeoutId) {
          //clear pending task
          clearTimeout(timeoutId);
        }
        timeoutId = setTimeout(function () {
          if (lastKeyword === _keywords) {
            return;
          }
          ztreeFilter(zTreeObj, _keywords); //lazy load ztreeFilter function
          lastKeyword = _keywords;
        }, 20);
      }
    },
    showTree(i) {
      let that = this;
      if (i == 1) {
        this.sheetdic.dickey = "DIC_RS_ZB01";
        this.sheetdic.inputValue = this.seachObj.b0117;
        this.sheetdic.showDicPanel = true;
        this.sheetdic.searchValue = function (valInfo) {
          that.seachObj.b0117BDIC = valInfo.dispalyVal;
          that.seachObj.b0117 = valInfo.value;
          //  this.$dicManager.getName("RS_UN_UM",this.editObj.editRight.other.NID)
        }
      }
      if (i == 2) {
        this.sheetdic.dickey = "DIC_RS_ZB87";
        this.sheetdic.inputValue = this.seachObj.b0124;
        this.sheetdic.showDicPanel = true;
        this.sheetdic.searchValue = function (valInfo) {
          that.seachObj.b0124DIC = valInfo.dispalyVal;
          that.seachObj.b0124 = valInfo.value;
          //  this.$dicManager.getName("RS_UN_UM",this.editObj.editRight.other.NID)
        }
      }
      if (i == 3) {
        this.sheetdic.dickey = "DIC_RS_ZB04";
        this.sheetdic.inputValue = this.seachObj.b0131;
        this.sheetdic.showDicPanel = true;
        this.sheetdic.searchValue = function (valInfo) {
          that.seachObj.b0131DIC = valInfo.dispalyVal;
          that.seachObj.b0131 = valInfo.value;
          //  this.$dicManager.getName("RS_UN_UM",this.editObj.editRight.other.NID)
        }
      }
      if (i == 4) {
        this.sheetdic.dickey = "DIC_RS_ZB03";
        this.sheetdic.inputValue = this.seachObj.b0127;
        this.sheetdic.showDicPanel = true;
        this.sheetdic.searchValue = function (valInfo) {
          that.seachObj.b0127DIC = valInfo.dispalyVal;
          that.seachObj.b0127 = valInfo.value;
          //  this.$dicManager.getName("RS_UN_UM",this.editObj.editRight.other.NID)
        }
      }

    },
    clearSeach() {
      this.seachObj = {};
    },
    seachTable() {
      this.seachObj.AP = "2";
      this.$emit("treeNodeClick", this.seachObj);
    }
  }
};
</script>
